package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhao
 * @Date: 2020/12/4 15:33
 */
@Api(description = "期初库存")
@RestController
public class InventoryController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     * 请求URL：http://localhost:8080/goods/getNoInventoryQuantity
     * 请求参数：Integer page,Integer rows,String nameOrCode
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("/goods/getNoInventoryQuantity")
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsService.getNoInventoryQuantity(page, rows, nameOrCode);

        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", goodsList);
        return map;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * 请求URL：http://localhost:8080/goods/getHasInventoryQuantity
     * 请求参数：Integer page,Integer rows,String nameOrCode
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("/goods/getHasInventoryQuantity")
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsService.getHasInventoryQuantity(page, rows, nameOrCode);

        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", goodsList);
        return map;
    }

    /**
     * 添加库存、修改数量或成本价
     * 请求URL：http://localhost:8080/goods/saveStock?goodsId=25
     * 请求参数：Integer goodsId,Integer inventoryQuantity,double purchasingPrice
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/goods/saveStock")
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        return goodsService.saveStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * 请求URL：http://localhost:8080/goods/deleteStock
     * 请求参数：Integer goodsId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/goods/deleteStock")
    public ServiceVO deleteStock(Integer goodsId) {
        return goodsService.deleteStock(goodsId);
    }

}
